package GUI;

import javax.swing.*;
import Data.*;

public class Attribute extends AttributeMethod {
	public Attribute(String text, ClassFrame classFrame) {
		super(text, classFrame);
		typeLabel.setText("-");
	}
	
	public Attribute(Attribute a, ClassFrame classFrame) {
		super(a, classFrame);
		typeLabel.setText("-");
	}
	
	public void nameTestField_EnterKeyPressed() {
		if (nameTextField.isEditable()) {
			boolean success = false;
			Data.Class c = classFrame.classData;
			
			if (nameTextField.getText().equals(nameTextField.getName())) {
				nameTextField.setEditable(false);
				nameTextField.setBackground(BACKGROUND);
				return;
			}
			
			success = c.canUpdateAttribute(nameTextField.getText());

			if (success) {
				log.push(new ClassWithShape(c, classFrame));
				nameTextField.setEditable(false);
				nameTextField.setBackground(BACKGROUND);
				
				if (nameTextField.getName() != null) {
					c.updateAttribute(nameTextField.getName(), nameTextField.getText());
				} else {
					c.addAttribute(nameTextField.getText());
				}
				nameTextField.select(nameTextField.getText().length(), nameTextField.getText().length());
				nameTextField.setName(nameTextField.getText());
				log.push(new ClassWithShape(c, classFrame));
				
			} else {
				Message.showMessage(lang.getString("AttributeMethod.message.setUpdateError"));
			}

		} else {
			nameTextField_actived();
		}
	}
	
	public void deleteButton_Clicked() {
		Data.Class c = classFrame.classData;
		if (nameTextField.getName() != null) {
			c.removeAttribute(nameTextField.getName());
		}
		JPanel tmp = (JPanel)me.getParent();
		tmp.remove(me);
		tmp.updateUI();
		log.push(new ClassWithShape(c, classFrame));
	}
}
